﻿@using Sovigaz
@{
    ViewBag.Title = "Chi tiết người sử dụng";
    var url = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');
}
<script>
    app.controller('UserController', function ($scope, $compile, $http, $filter, service) {
        $scope.loadRole = function () {
            var url = '@url' + '@Url.Action("RoleApi", "Api")';
            $http.get(url)
                .success(function (data) {
                    $scope.roles = data;
                })
                .error(function (data) {
                    service.showModal($scope, $compile, 'error', data);
                });
        };

        $scope.loadPosition = function () {
            var url = '@url' + '@Url.Action("PositionApi", "Api")';
            $http.get(url)
                .success(function (data) {
                    for(var i=0;i<data.length;i++)
                        $scope.positions.push({"name": data[i].PositionName, "value": data[i].PositionID});
                })
                .error(function (data) {
                    service.showModal($scope, $compile, 'error', data);
                });
        };

        $scope.load = function () {
            startloading($('#content'));

            var url = '@url' + '@Url.Action("UserApi", "Api", new {id = Model})';

            $http.get(url)
                .success(function (data) {
                    $scope.user = data;
                    $scope.position = { name: '', value: data.Position };
                    for (var i = 0; i < data.UserRoles.length; i++) {
                        $scope.ur[data.UserRoles[i].RoleID] = true;
                    }

                    stoploading($('#content'));
                })
                .error(function (data) {
                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'error', data);
                });
        };

        $scope.refresh = function () {
            $scope.load();
        };

        $scope.add = function () {
            window.location = '@Url.Action("Detail", "User", new { id = String.Empty })';
        };

        $scope.save = function () {
            $scope.user.Position = $scope.position.value;
            $scope.user.UserRoles = [];

            for (var s in $scope.ur) {
                if (s !== '0' && $scope.ur[s] === true) {
                    $scope.user.UserRoles.push({ "UserID": $scope.user.UserID, "RoleID": s });
                }
            }
            $scope.user.StartDate = $filter('date')(Date.parse($scope.user.StartDate), 'y/MM/dd');
            $scope.user.Description = $filter('date')(Date.parse($scope.user.StartDate), 'y/MM/dd') + "---" + Date.parse($scope.user.StartDate);
            var url = '@url' + '@Url.Action("UserApi", "Api")';
            $http.put(url, $scope.user)
                .success(function(data) {
                    if (@Model === 0) {
                        window.location = '@Url.Action("Detail", "User")/' + data.UserID;
                    }
                    $scope.user = data;

                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'notify', 'Dữ liệu đã được lưu thành công!!!');
                })
                .error(function(data) {
                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'error', data);
                });
        };

        $scope.confirmDelete = function () {
            service.showModal($scope, $compile, 'confirm', 'Bạn có chắc chắn muốn xóa dữ liệu?', 'delete()');
        };

        $scope.delete = function () {
            var url = '@url' + '@Url.Action("UserApi", "Api")';
            $http.delete(url, {
                params: {
                    id: $scope.user.UserID
                }
            }).success(function (data) {
                stoploading($('#content'));
                window.location = '@Url.Action("Detail", "User", new { id = String.Empty })';
            }).error(function (data) {
                stoploading($('#content'));
                service.showModal($scope, $compile, 'error', data);
            });
        };

        $scope.back = function () {
            window.location = '@Url.Action("Index", "User")';
        };

        $scope.ur = [{}];
        $scope.positions = [{}];
        $scope.position = {};
        $scope.loadRole();
        $scope.loadPosition();
        $scope.load();
    });

    $(function () {
        $("[data-toggle=tooltip]").tooltip();
    });
</script>

<div ng-controller="UserController">
    @Html.Partial("_PageHeader")
    @Html.Partial("_Toolbar")
    <div id="content" class="content row">
        <form role="form" name="form" autocomplete="off">
            <div class="panel-body">
                <div class="col-lg-4">
                    @Html.Partial("Control/_Control_TextBox_Required", new Dictionary<string, string>
                    {
                        {"Text", "Tên người sử dụng"},
                        {"Model", "user.FullName"},
                        {"Name", "fullName"},
                        {"Message", "Tên người sử dụng bắt buộc nhập."}
                    })
                </div>
                <div class="col-lg-4">
                    @Html.Partial("Control/_Control_ComboBox", new Dictionary<string, string>
                    {
                        {"Text", "Chức vụ"},
                        {"Model", "position"},
                        {"Name", "position"},
                        {"Options", "pos.name for pos in positions track by pos.value"}
                    })
                </div>
                <div class="col-lg-4" style="clear: both">
                    @Html.Partial("Control/_Control_TextBox", new Dictionary<string, string>
                    {
                        {"Text", "Tên đăng nhập"},
                        {"Model", "user.Username"},
                        {"Name", "userName"}
                    })
                </div>
                <div class="col-lg-4">
                    <label>Mật khẩu</label>
                    <div class="form-group">
                        <input ng-model="user.Password" name="password" type="password" class="form-control" autocomplete="off" />
                    </div>
                </div>
                <div class="col-lg-4" style="clear: both">
                    @Html.Partial("Control/_Control_Datepicker", new Dictionary<string, string>
                    {
                        {"Text", "Ngày bắt đầu"},
                        {"Model", "user.StartDate"},
                        {"Name", "startDate"}
                    })
                </div>
                <div class="col-lg-4">
                    @Html.Partial("Control/_Control_CheckBox", new Dictionary<string, string>
                    {
                        {"Text", "Đang làm việc"},
                        {"Model", "user.IsActived"}
                    })
                </div>
                <div class="col-lg-8" style="clear: both">
                    @Html.Partial("Control/_Control_TextArea", new Dictionary<string, string>
                    {
                        {"Text", "Ghi chú"},
                        {"Model", "user.Description"},
                        {"Name", "description"}
                    })
                </div>
                <div class="col-lg-8" style="clear: both; border-top: 1px solid lightgray; margin-top: 10px; margin-bottom: 10px">

                </div>
                <div class="col-lg-8" style="clear: both">
                    <b>Danh sách vai trò</b>
                </div>
                <div class="col-lg-8" style="clear: both">
                    <div class="table-responsive">
                        <table class="table table-striped table-bordered table-hover">
                            <colgroup>
                                <col />
                                <col style="width: 100px" />
                            </colgroup>
                            <thead>
                                <tr>
                                    <th>
                                        Vai trò
                                    </th>
                                    <th class="center">

                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ng-repeat="role in roles">
                                    <td>{{role.RoleName}}</td>
                                    <td class="center"><input ng-model="ur[role.RoleID]" type="checkbox" /></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>